﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Security;
using WebApplicationApi.Utilities.Filter;

namespace WebApplicationApi.Controllers
{
  public class UserController : ApiController
  {
    // GET api/values
    public IEnumerable<string> Get()
    {
      return new string[] { "value1", "value2" };
    }
    [HttpGet]
    [Route("api/user/login")]
    public string Login(string account, string password)
    {
      if (account.Equals("admin") && "123456".Equals(password))
      {
        FormsAuthenticationTicket ticketObj = new FormsAuthenticationTicket(0, account, DateTime.Now, DateTime.Now.AddHours
          (1), true, $"userName:{account},password:{password}", FormsAuthentication.FormsCookiePath);
        var result = new { Result = true, Ticket = FormsAuthentication.Encrypt(ticketObj) };
        return JsonConvert.SerializeObject(result);
      }
      return JsonConvert.SerializeObject(new { Result = false });
    }
    [CustomAuthorizeAttribute]
    [HttpGet]
    [Route("api/user/index")]
    public string Index()
    {
      return "happy";
    }
  }
}